package com.carsa.blackberry.ui.screen;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;

import com.carsa.blackberry.CarsaApp;
import com.carsa.blackberry.ui.UIKeys;
import com.carsa.blackberry.ui.screen.common.BasicScreen;
import com.carsa.blackberry.util.ui.UIUtil;
import com.carsa.blackberry.util.ui.widget.GridFieldManager;

/**
 * @author Nahuel Barrios.
 * @since 13/08/2010.
 * @version 1.0
 */
public class LoadingScreen extends BasicScreen {

    private GaugeField _progressBar = new GaugeField();

    public LoadingScreen() {
	super(UIKeys.TITLE_CARGANDO);
	super.customizeScreen();

	final BitmapField img_megatone = new BitmapField(Bitmap.getBitmapResource(UIKeys.IMG_MEGATONE_ICON_AND_TEXT),
	        FIELD_LEFT | FIELD_VCENTER);
	final BitmapField img_carsa = new BitmapField(Bitmap.getBitmapResource(UIKeys.IMG_GRUPO_CARSA_ICON_SMALL),
	        FIELD_RIGHT);
	GridFieldManager grid_top = new GridFieldManager(new int[] { 260, 60 }, 0);
	grid_top.add(img_megatone);
	grid_top.add(img_carsa);

	_progressBar = new GaugeField("", 0, 100, 0, GaugeField.PERCENT) {
	    protected void paintBackground(Graphics graphics) {
		graphics.setBackgroundColor(UIKeys.COLOR_APP_BLACK);// Black

		graphics.clear();
		super.paint(graphics);
	    }
	};

	_mainManager.add(grid_top);
	_mainManager.add(UIUtil.getEmptyLine());
	_mainManager.add(UIUtil.getEmptyLine());
	_mainManager.add(UIUtil.getEmptyLine());
	_mainManager.add(UIUtil.getEmptyLine());
	_mainManager.add(_progressBar);
	_mainManager.add(UIUtil.getEmptyLine());
	_mainManager.add(UIUtil.getEmptyLine());

	this.add(_mainManager);

    }

    public void putContinueButton() {
	this.setProgressBarValue(100);

	UiApplication.getUiApplication();
	synchronized (Application.getEventLock()) {
	    // UI Code here

	    ButtonField button = new ButtonField(UIKeys.TITLE_CONTINUAR, Field.FIELD_HCENTER);
	    button.setChangeListener(new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
		    if (_progressBar.getValue() == 100) {
			doBeforeLeaveScreen();
			UiApplication.getUiApplication().pushScreen(new LoginScreen());
		    }
		}
	    });
	    _mainManager.add(button);
	}
    }

    public void putCloseAppButton() {
	UiApplication.getUiApplication();
	synchronized (Application.getEventLock()) {
	    // UI Code here

	    CarsaApp.getUIThread().getLoadingScreen().add(
		    new LabelField(UIKeys.ERROR_NO_HAY_UNA_CONEXION_DISPONIBLE_LA_APP_DEBE_CERRARSE));

	    ButtonField btn_closeApplication = new ButtonField(UIKeys.TITLE_CLOSE_APPLICATION, Field.FIELD_HCENTER);
	    btn_closeApplication.setChangeListener(new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
		    System.exit(0);
		}
	    });

	    CarsaApp.getUIThread().getLoadingScreen().add(btn_closeApplication);
	}
    }

    public boolean onClose() {
	return false;
    }

    protected boolean doBeforeLeaveScreen() {
	UiApplication.getUiApplication().popScreen(this);
	return true;
    }

    public void setProgressBarValue(int value) {
	_progressBar.setValue(value);
    }

    public int getProgressBarValue() {
	return _progressBar.getValue();
    }

    public void putNextScreen() {
	doBeforeLeaveScreen();
	UiApplication.getUiApplication().pushScreen(new LoginScreen());
    }

}
